<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Recyled {

	public function start() {
		global $cache,$request;

		$action = $request->get( 'p' );

		switch( $action ) {
			case 'BatcharticleExe':
				$this->batch_exe( 'article' );
				break;
			case 'BatchcommentExe':
				$this->batch_exe( 'comment' );
				break;
			case 'BatchuserExe':
				$this->batch_exe( 'user' );
				break;
			case 'BatchlinkExe':
				$this->batch_exe( 'link' );
				break;
			default:
				$this->show( $action );
		}
	}

	private function batch_exe( $type ) {
		global $_CACHE,$db;
		$control = getGP( 'Control','P' );
		$sql = getGP( 'SQL','P' );
		if( $type == 'article' ) {
			if( $control == '删除' ) {
				// 删除日志
				$db->query( "DELETE FROM `" . DB_PREFIX . "article` WHERE ( $sql )" );
				// 删除对应评论
				$id_arr = str_replace( 'id=','',$sql );
				$id_arr = split( ' OR ',$id_arr );
				$size = count( $id_arr );
				$sql = '';
				for( $i=0; $i<$size; $i ++ ){
					$sql .= 'articleid=' . $id_arr[$i] . ' OR ';
				}
				$sql = substr( $sql,0,-4 );
				$db->query( "DELETE FROM `" . DB_PREFIX . "comment` WHERE ( $sql )" );
				show_admin_message( '批量删除日志成功', 'admin_recyled.php?action=article' );
			}else{
				// 恢复日志
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET isdel=0 WHERE ( $sql )" );
				// 恢复标签
				$tag_arr = $db->fetch_all( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE ( $sql )" );
				foreach( $tag_arr as $tags ) {
					$tags['tag'] = str_replace( '*}{*',',',$tags['tag'] );
					$tags['tag'] = str_replace( '*}','',$tags['tag'] );
					$tags['tag'] = str_replace( '{*','',$tags['tag'] );
					$tags['tag'] = split( ',' , $tags['tag'] );
					foreach($tags['tag'] as $tag){
						if( $tag != '' ){
							$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num=t_num+1 WHERE t_id=$tag" );
						}
					}
				}
				// 恢复评论
				$id_arr = str_replace( 'id=','',$sql );
				$id_arr = split( ' OR ',$id_arr );
				$size = count( $id_arr );
				$sql = '';
				for( $i=0; $i<$size; $i ++ ){
					$sql .= 'articleid=' . $id_arr[$i] . ' OR ';
				}
				$sql = substr( $sql,0,-4 );
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel=0 WHERE ( $sql )" );
				// 重新统计分类数据
				foreach( $_CACHE['category'] as $category ) {
					$cid = $category['cid'];
					$article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND category=$cid" );
					if( $category['articlenum'] != $article_num ) {
						$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum=$article_num WHERE cid=$cid" );
					}
				}
				// 刷新缓存
				cache_article_hot();
				cache_article_new();
				cache_comment_new();
				cache_category();
				cache_count();
				cache_tag();
				show_admin_message( '批量恢复日志成功', 'admin_recyled.php?action=article' );
			}
		}elseif( $type == 'comment' ) {
			if( $control == '删除' ) {
				$aid_arr = $db->fetch_all( "SELECT articleid FROM `" . DB_PREFIX . "comment` WHERE ( $sql )" );
				foreach( $aid_arr as $aid ) {
					$db->query( "UPDATE `" . DB_PREFIX . "article` SET comment=comment-1 WHERE id=" . $aid['articleid'] . " AND isdel=1" );
				}
				$db->query( "DELETE FROM `" . DB_PREFIX . "comment` WHERE ( $sql )" );
				show_admin_message( '批量删除评论成功', 'admin_recyled.php?action=comment' );
			}else{
				$info_arr = $db->fetch_all( "SELECT cid,articleid FROM `" . DB_PREFIX . "comment` WHERE ( $sql )" );
				foreach( $info_arr as $info ) {
					if( $info['articleid'] == 0 ) {
						$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel=0 WHERE cid=" . $info['cid'] );
						continue;
					}
					if( $db->result( "SELECT isdel FROM `" . DB_PREFIX . "article` WHERE id=" . $info['articleid'] ) == 0 ) {
						$db->query( "UPDATE `" . DB_PREFIX . "article` SET comment=comment+1 WHERE id=" . $info['articleid'] );
						$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel=0 WHERE cid=" . $info['cid'] );
					}
				}
				// 刷新缓存
				cache_article_hot();
				cache_comment_new();
				cache_guestbook_new();
				cache_count();
				show_admin_message( '批量恢复评论成功', 'admin_recyled.php?action=comment' );
			}
		}elseif( $type == 'user' ) {
			if( $control == '删除' ) {
				$db->query( "DELETE FROM `" . DB_PREFIX . "user` WHERE  ( $sql )" );
				show_admin_message( '批量删除用户成功', 'admin_recyled.php?action=user' );
			}else{
				$db->query( "UPDATE `" . DB_PREFIX . "user` SET u_isdel=0 WHERE  ( $sql )" );
				// 刷新缓存
				cache_count();
				show_admin_message( '批量恢复用户成功', 'admin_recyled.php?action=user' );
			}
		}else{
			if( $control == '删除' ) {
				$db->query( "DELETE FROM `" . DB_PREFIX . "link` WHERE  ( $sql )" );
				show_admin_message( '批量删除链接成功', 'admin_recyled.php?action=link' );
			}else{
				$db->query( "UPDATE `" . DB_PREFIX . "link` SET isdel=0 WHERE  ( $sql )" );
				// 刷新缓存
				cache_link();
				show_admin_message( '批量恢复链接成功', 'admin_recyled.php?action=link' );
			}
		}
	}

	private function show( $action, $message = '', $type = '' ) {
		global $cache;
		if( $message != '' ) {
			$message = '<p class="' . $type . '">' . $message . '</p>';
		}
		@header("content-type: text/html; charset=utf-8");
?>
<div class = "admin_panel">
	<div class = "admin_title">回收站</div>
	<div class = "admin_content">
		<?php
		echo $message;
		switch( $action ) {
			case 'article':
				$this->article_list();
				break;
			case 'Batcharticle':
				$this->article_list( 'batch' );
				break;
			case 'comment':
				$this->comment_list();
				break;
			case 'Batchcomment':
				$this->comment_list( 'batch' );
				break;
			case 'user':
				$this->user_list();
				break;
			case 'Batchuser':
				$this->user_list( 'batch' );
				break;
			case 'link':
				$this->link_list();
				break;
			case 'Batchlink':
				$this->link_list( 'batch' );
				break;
			default:
				$this->main();
		}
	?>
	</div>
</div>
<?php
	}

	private function article_list( $action = 'list' ) {
		global $db,$_CACHE;
		if( $action == 'batch' ) {
			$id_arr = array();
			$id_arr = getGP( 'id_arr','P','array' );
			$control = getGP( 'Control','P' );
			$size = count( $id_arr );
			if( $size == 0 ) {
				obclean();
				show_admin_message( '您没有选中任何日志', 'admin_recyled.php?action=article' );
				exit;
			}
			$sql = '';
			for( $i=0; $i<$size; $i ++ ){
				$sql .= 'id=' . $id_arr[$i] . ' OR ';
			}
			$sql = substr( $sql,0,-4 );
			$article_array = $db->fetch_all( 'SELECT id,author,title,time,isdel,category,`read`,comment,`type` FROM `' . DB_PREFIX . 'article` WHERE isdel=1 AND (' . $sql . ') ORDER BY id DESC' );
		}else{
			$article_array = $db->fetch_all( 'SELECT id,author,title,time,isdel,category,`read`,comment,`type` FROM `' . DB_PREFIX . 'article` WHERE isdel=1 ORDER BY id DESC' );
		}
?>
<?php if( $action == 'list' ) { ?><form name="smslist" action="admin_recyled.php?action=Batcharticle" method="post"><?php } ?>
	<div style="padding:5px;margin:2px;">      
		<table <?php if( $action == 'list' ) { ?>class="pickme"<?php } ?> style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
			<thead>
				<tr align="center" height="22" bgcolor="#ffffff">
					<td width="3%"><?php if( $action == 'list' ) { ?><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled><?php } ?></td>
					<td width="4%">序号</td>
					<td width="10%">作者</td>
					<td width="26%">标题</td>
					<td width="6%">类型</td>
					<td width="8%">分类</td>
					<td width="8%">阅读</td>
					<td width="8%">评论</td>
					<td width="14%">时间</td>
					<td width="13%">管理操作</td>
				</tr>
			</thead>
			<tbody>
			<?php
			$i = 1;
			foreach( $article_array as $value ) {
			?>
				<tr align="center" height="20" bgcolor="#ffffff">
					<td ><?php if( $action == 'list' ) { ?><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['id'] ?>" /><?php } ?></td>
					<td><?php echo $i ?></td>
					<td><?php echo $value['author'] ?></td>
					<td align="left"><?php echo $value['title'] ?></td>
					<td><?php echo $value['type'] ?></td>
					<td><?php echo $_CACHE['category'][$value['category']]['name'] ?></td>
					<td><?php echo $value['read'] ?></td>
					<td><?php echo $value['comment'] ?></td>
					<td><?php echo date( 'Y-m-d', $value['time'] ) ?></td>
					<td></td>
				</tr>
			<?php
				$i ++;
			}
			?>
			</tbody>
		</table>
	</div>
	<?php if( $action == 'list' ) { ?>
	<div align="center">批量管理选项: <input type="radio" name="Control" value="删除" checked="checked"/>删除 <input type="radio" name="Control" value="恢复" />恢复 <input type="submit" name="functionmit" class="main_button" value="执行操作" /></div>
	<?php }else{ ?>
	<div style="padding:5px;margin:2px;">
		<form name="smslist" action="admin_recyled.php?action=BatcharticleExe" method="post">
			<input type="hidden" name="SQL" value="<?php echo $sql ?>"/>
			<?php if( $control == '删除' ) { ?>
			<input type="hidden" name="Control" value="删除"/>
			<b>彻底删除后将不可恢复！彻底删除日志的同时也将彻底删除日志所对应的评论。确认要删除吗？</b><br />
			<input type="submit" name="submit" class="main_button" value="确认删除" />
			<?php }else{ ?>
			<input type="hidden" name="Control" value="恢复"/>
			<b>将同时恢复此日志所对应的评论。确认要恢复吗？</b><br />
			<input type="submit" name="submit" class="main_button" value="确认恢复" />
			<?php } ?>
		</form>
	</div>
	<?php } ?>
<?php if( $action == 'list' ) { ?></form><?php } ?>
<div class="main_button"></div>
<?php
	}

	private function comment_list( $action = 'list' ) {
		global $db,$_CACHE;
		if( $action == 'batch' ) {
			$id_arr = array();
			$id_arr = getGP( 'id_arr','P','array' );
			$control = getGP( 'Control','P' );
			$size = count( $id_arr );
			if( $size == 0 ) {
				obclean();
				show_admin_message( '您没有选中任何评论', 'admin_recyled.php?action=comment' );
				exit;
			}
			$sql = '';
			for( $i=0; $i<$size; $i ++ ){
				$sql .= 'cid=' . $id_arr[$i] . ' OR ';
			}
			$sql = substr( $sql,0,-4 );
			$comment_array = $db->fetch_all( 'SELECT cid,author,time,comment,reply,replytime FROM `' . DB_PREFIX . 'comment` WHERE isdel=1 AND (' . $sql . ') ORDER BY cid DESC' );
		}else{
			$comment_array = $db->fetch_all( 'SELECT cid,author,time,comment,reply,replytime FROM `' . DB_PREFIX . 'comment` WHERE isdel=1 ORDER BY cid DESC' );
		}
?>
			<?php if( $action == 'list' ) { ?><form name="smslist" action="admin_recyled.php?action=Batchcomment" method="post"><?php } ?>
				<div style="padding:5px;margin:2px;">      
					<table <?php if( $action == 'list' ) { ?>class="pickme"<?php } ?> style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
						<thead>
							<tr align="center" height="22" bgcolor="#ffffff">
								<td width="3%"><?php if( $action == 'list' ) { ?><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled><?php } ?></td>
								<td width="4%">序号</td>
								<td width="10%">作者</td>
								<td width="31%">内容</td>
								<td width="9%">时间</td>
								<td width="20%">回复</td>
								<td width="10%">回复时间</td>
								<td width="13%">管理操作</td>
							</tr>
						</thead>
						<tbody>
							<?php
							$i = 1;
							foreach( $comment_array as $value ) {
							?>
							<tr align="center" height="20" bgcolor="#ffffff">
								<td ><?php if( $action == 'list' ) { ?><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['cid'] ?>" /><?php } ?></td>
								<td><?php echo $i ?></td>
								<td><?php echo $value['author'] ?></td>
								<td align="left"><?php echo $value['comment'] ?></td>
								<td><?php echo date( 'Y-m-d', $value['time'] ) ?></td>
								<td align="left"><?php echo $value['reply'] ?></td>
								<td><?php if( $value['reply'] != '' ) echo date( 'Y-m-d', $value['replytime'] ); ?>
								</td>
								<td></td>
							</tr>
							<?php
							$i ++;
							}
							?>
						</tbody>
					</table>
				</div>
			<?php if( $action == 'list' ) { ?>
				<div align="center">批量管理选项: <input type="radio" name="Control" value="删除" checked="checked"/>删除 <input type="radio" name="Control" value="恢复" />恢复 <input type="submit" name="Submit" class="main_button" value="执行操作" /></div>
			<?php }else{ ?>
				<div style="padding:5px;margin:2px;">
				<form name="smslist" action="admin_recyled.php?action=BatchcommentExe" method="post">
					<input type="hidden" name="SQL" value="<?php echo $sql ?>"/>
				<?php
				if( $control == '删除' ) {
				?>
					<input type="hidden" name="Control" value="删除"/>
					<b>彻底删除后将不可恢复！确认要删除吗？</b><br />
					<input type="submit" name="submit" class="main_button" value="确认删除" />
				<?php
				}else{
				?>
					<input type="hidden" name="Control" value="恢复"/>
					<b>如果对应的日志处于被删除状态，则评论不会被恢复。</b><br />
					<input type="submit" name="submit" class="main_button" value="确认恢复" />
				<?php
				}
				?>
				</form>
				</div>
			<?php } ?>
			<?php if( $action == 'list' ) { ?></form><?php } ?>
			<div class="main_button"></div>
<?php
	}

	private function user_list( $action = 'list' ) {
		global $db,$_CACHE;
		if( $action == 'batch' ) {
			$id_arr = array();
			$id_arr = getGP( 'id_arr','P','array' );
			$control = getGP( 'Control','P' );
			$size = count( $id_arr );
			if( $size == 0 ) {
				obclean();
				show_admin_message( '您没有选中任何用户', 'admin_recyled.php?action=user' );
				exit;
			}
			$sql = '';
			for( $i=0; $i<$size; $i ++ ){
				$sql .= 'u_id=' . $id_arr[$i] . ' OR ';
			}
			$sql = substr( $sql,0,-4 );
			$user_array = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'user` WHERE (' . $sql . ')' );
		}else{
			$user_array = $db->fetch_all( "SELECT * FROM `" . DB_PREFIX . "user` WHERE u_isdel=1" );
		}
?>
			<?php if( $action == 'list' ) { ?><form name="smslist" action="admin_recyled.php?action=Batchuser" method="post"><?php } ?>
				<div style="padding:5px;margin:2px;">
					<table <?php if( $action == 'list' ) { ?>class="pickme"<?php } ?> style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
						<thead>
							<tr align="center" height="22" bgcolor="#ffffff">
								<td width="3%"><?php if( $action == 'list' ) { ?><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled><?php } ?></td>
								<td width="4%">序号</td>
								<td width="10%">用户名</td>
								<td width="8%">用户组</td>
								<td width="16%">电子邮箱</td>
								<td width="10%">Q Q</td>
								<td width="13%">主页</td>
								<td width="10%">上次登录ip</td>
								<td width="13%">上次登录时间</td>
								<td width="13%">管理操作</td>
							</tr>
						</thead>
						<tbody>
							<?php
							$i = 1;
							foreach( $user_array as $value ) {
								$value['u_email'] = split( "\|", $value['u_email'] );
							?>
							<tr align="center" height="20" bgcolor="#ffffff">
								<td ><?php if( $action == 'list' ) { ?><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['u_id'] ?>" /><?php } ?></td>
								<td><?php echo $i ?></td>
								<td><?php echo $value['u_username'] ?></td>
								<td><?php if( $value['u_group'] > 7 ) { echo '管理员'; }else{ echo '注册用户'; } ?></td>
								<td><?php echo $value['u_email'][0] ?></td>
								<td><?php if( $value['u_qqnum'] == 0 ) { echo ''; }else{ echo $value['u_qqnum']; } ?></td>
								<td><?php echo $value['u_index'] ?></td>
								<td><?php echo $value['u_ip'] ?></td>
								<td><?php echo date('Y-m-d',$value['u_time']) ?></td>
								<td></td>
							</tr>
								<?php
								$i ++;
							}
							?>
						</tbody>
					</table>
				</div>
			<?php if( $action == 'list' ) { ?>
				<div align="center">批量管理选项: <input type="radio" name="Control" value="删除" checked="checked"/>删除 <input type="radio" name="Control" value="恢复" />恢复 <input type="submit" name="Submit" class="main_button" value="执行操作" /></div>
			<?php }else{ ?>
				<div style="padding:5px;margin:2px;">
				<form name="smslist" action="admin_recyled.php?action=BatchuserExe" method="post">
					<input type="hidden" name="SQL" value="<?php echo $sql ?>"/>
				<?php
				if( $control == '删除' ) {
				?>
					<input type="hidden" name="Control" value="删除"/>
					<b>彻底删除后将不可恢复！确认要删除吗？</b><br />
					<input type="submit" name="submit" class="main_button" value="确认删除" />
				<?php
				}else{
				?>
					<input type="hidden" name="Control" value="恢复"/>
					<input type="submit" name="submit" class="main_button" value="确认恢复" />
				<?php
				}
				?>
				</form>
				</div>
			<?php } ?>
			<?php if( $action == 'list' ) { ?></form><?php } ?>
			<div class="main_button"></div>
<?php
	}

	private function link_list( $action = 'list' ) {
		global $db,$_CACHE;
		if( $action == 'batch' ) {
			$id_arr = array();
			$id_arr = getGP( 'id_arr','P','array' );
			$control = getGP( 'Control','P' );
			$size = count( $id_arr );
			if( $size == 0 ) {
				obclean();
				show_admin_message( '您没有选中任何链接', 'admin_recyled.php?action=link' );
				exit;
			}
			$sql = '';
			for( $i=0; $i<$size; $i ++ ){
				$sql .= 'lid=' . $id_arr[$i] . ' OR ';
			}
			$sql = substr( $sql,0,-4 );
			$link_array = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'link` WHERE (' . $sql . ')' );
		}else{
			$link_array = $db->fetch_all( "SELECT * FROM `" . DB_PREFIX . "link` WHERE isdel=1" );
		}
?>
			<?php if( $action == 'list' ) { ?><form name="smslist" action="admin_recyled.php?action=Batchlink" method="post"><?php } ?>
				<div style="padding:5px;margin:2px;">      
					<table <?php if( $action == 'list' ) { ?>class="pickme"<?php } ?> style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
						<thead>
							<tr align="center" height="22" bgcolor="#ffffff">
								<td width="3%"><?php if( $action == 'list' ) { ?><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled><?php } ?></td>
								<td width="4%">序号</td>
								<td width="24%">链接名称</td>
								<td width="30%">链接描述</td>
								<td width="26%">链接地址</td>
								<td width="13%">管理操作</td>
							</tr>
						</thead>
						<tbody>
							<?php
							$i = 1;
							foreach( $link_array as $value ) {
							?>
							<tr align="center" height="20" bgcolor="#ffffff">
								<td ><?php if( $action == 'list' ) { ?><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['lid'] ?>" /><?php } ?></td>
								<td><?php echo $i ?></td>
								<td><?php echo $value['title'] ?></td>
								<td><?php echo $value['description'] ?></td>
								<td><?php echo $value['address'] ?></td>
								<td></td>
							</tr>
							<?php
							$i ++;
							}
							?>
						</tbody>
					</table>
				</div>
			<?php if( $action == 'list' ) { ?>
				<div align="center">批量管理选项: <input type="radio" name="Control" value="删除" checked="checked"/>删除 <input type="radio" name="Control" value="恢复" />恢复 <input type="submit" name="Submit" class="main_button" value="执行操作" /></div>
			<?php }else{ ?>
				<div style="padding:5px;margin:2px;">
				<form name="smslist" action="admin_recyled.php?action=BatchlinkExe" method="post">
					<input type="hidden" name="SQL" value="<?php echo $sql ?>"/>
				<?php
				if( $control == '删除' ) {
				?>
					<input type="hidden" name="Control" value="删除"/>
					<b>彻底删除后将不可恢复！确认要删除吗？</b><br />
					<input type="submit" name="submit" class="main_button" value="确认删除" />
				<?php
				}else{
				?>
					<input type="hidden" name="Control" value="恢复"/>
					<input type="submit" name="submit" class="main_button" value="确认恢复" />
				<?php
				}
				?>
				</form>
				</div>
			<?php } ?>
			<?php if( $action == 'list' ) { ?></form><?php } ?>
			<div class="main_button"></div>
<?php
	}

	private function main() {
?>
<div style="padding:10px;">      
	欢迎来到 <b>回收站</b>，在这里您可以恢复被删除的日志、评论、链接和用户。
</div>
<?php
	}

}
?>
